package com.zhuangjie.base.vo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 分页数据统一输出
 *
 * @author zhuangjie
 * @date 2023/12/12
 */
@Data
public class PageVO<T> {
    private Long total;
    private List<T> list;

    public PageVO() {
    }

    public PageVO(Page<T> page) {
        this.total = page.getTotal();
        this.list = page.getRecords();
    }

    /**
     * 将MybatisPlus Page解析为我们的PageVO
     *
     * @param shopVOPage 商店投票
     * @return {@link PageVO }<{@link R }>
     */
    public static <R> PageVO<R> parse(Page<R> shopVOPage) {
        PageVO<R> result = new PageVO<>();
        result.setTotal(shopVOPage.getTotal());
        List<R> records = shopVOPage.getRecords();
        result.setList(records == null ? Collections.emptyList() : records);
        return result;
    }
}
